package com.gemantic.task.runner;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gemantic.task.dao.repository.BaseTaskRepository;
import com.gemantic.task.dao.repository.impl.TaskRepositoryImpl;
import com.gemantic.task.handler.ITaskHandler;
import com.gemantic.task.handler.TaskHandlerFactory;
import com.gemantic.task.model.Task;
import com.gemantic.task.util.TaskUtil;

/**
 * @author xiedi
 * @date 2022/09/15
 **/
@Service
public class TaskRunner extends BaseTaskRunner<Task> {

    @Autowired
    protected TaskRepositoryImpl taskRepository;

    @Override
    protected BaseTaskRepository<Task> getTaskRepository() {
        return taskRepository;
    }

    @Override
    protected ITaskHandler getTaskHandler(Task task) {
        try {
            return getTaskHandlerFactory().getTaskHandler(task.getType());
        } catch (Exception ex) {
            logException(task, null, ex);
        }
        return null;
    }

    private TaskHandlerFactory getTaskHandlerFactory() {
        return TaskUtil.getTaskHandlerFactory();
    }
}
